package com.example.tys.baseapplication.NetModel.okhttp;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;


import com.example.tys.baseapplication.NetModel.network.ClientException;
import com.example.tys.baseapplication.NetModel.network.INetOperation;
import com.example.tys.baseapplication.NetModel.okhttp.requset.DeleteRequestBuilder;
import com.example.tys.baseapplication.NetModel.okhttp.requset.GetRequestBuilder;
import com.example.tys.baseapplication.NetModel.okhttp.requset.PostFileRequestBuilder;
import com.example.tys.baseapplication.NetModel.okhttp.requset.PostRequestBuilder;
import com.example.tys.baseapplication.NetModel.okhttp.requset.PostStringRequestBuilder;
import com.example.tys.baseapplication.NetModel.okhttp.requset.PutByteRequestBuilder;
import com.example.tys.baseapplication.NetModel.okhttp.requset.PutFileRequestBuilder;
import com.example.tys.baseapplication.NetModel.okhttp.requset.PutRequestBuilder;

import java.io.IOException;
import java.util.Map;

import okhttp3.Response;


/**
 * @author Administrator
 * @date 2016/10/26
 * OkHttp实现网络基本操作方法
 */
public class NetOperationOkHttp implements INetOperation<okhttp3.Response> {

    @Override
    public Response get(@NonNull String url, @Nullable Map<String, String> heads, @Nullable Map<String, String> params, @Nullable Object tag) throws IOException, ClientException {
        return new GetRequestBuilder()
                .url(url)
                .tag(tag)
                .heads(heads)
                .params(params)
                .build().execute();
    }

    @Override
    public Response delete(@NonNull String url, @Nullable Map<String, String> heads, @Nullable Map<String, String> params, @Nullable Object tag) throws IOException, ClientException {
        return new DeleteRequestBuilder()
                .url(url)
                .tag(tag)
                .heads(heads)
                .params(params)
                .build().execute();
    }

    @Override
    public Response post(@NonNull String url, @Nullable Map<String, String> heads, @Nullable Map<String, String> params, @Nullable Object tag) throws IOException, ClientException {
        return new PostRequestBuilder()
                .url(url)
                .tag(tag)
                .heads(heads)
                .params(params)
                .build().execute();
    }

    @Override
    public Response postString(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String context, @Nullable Object tag) throws IOException, ClientException {
        return new PostStringRequestBuilder()
                .url(url)
                .contextString(context)
                .heads(heads)
                .tag(tag)
                .build().execute();
    }

    @Override
    public Response postString(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String context, @Nullable Object tag,
                               @NonNull Integer connectTimeout, @NonNull Integer readTimeout, @NonNull Integer writeTimeout) throws IOException, ClientException {
        return new PostStringRequestBuilder()
                .url(url)
                .contextString(context)
                .heads(heads)
                .tag(tag)
                .connectTimeOut(connectTimeout)
                .readTimeOut(readTimeout)
                .writeTimeOut(writeTimeout)
                .build().execute();
    }

    @Override
    public Response putString(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String context, @Nullable Object tag) throws IOException, ClientException {
        return new PutRequestBuilder()
                .url(url)
                .contextString(context)
                .heads(heads)
                .tag(tag)
                .build().execute();
    }

    @Override
    public Response postFile(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String filePath, @Nullable Object tag) throws IOException, ClientException {
        return new PostFileRequestBuilder()
                .url(url)
                .heads(heads)
                .file(filePath)
                .tag(tag)
                .build().execute();
    }

    @Override
    public Response putFile(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String filePath, @Nullable Object tag) throws IOException, ClientException {
        return new PutFileRequestBuilder()
                .url(url)
                .heads(heads)
                .file(filePath)
                .tag(tag)
                .build().execute();
    }

    @Override
    public Response putByte(@NonNull String url, @Nullable Map<String, String> heads, @NonNull byte[] bytes, @Nullable Integer offset, @Nullable Integer byteCount, @Nullable Object tag) throws IOException, ClientException {
        return new PutByteRequestBuilder()
                .url(url)
                .heads(heads)
                .contentByte(bytes, offset, byteCount)
                .tag(tag)
                .build().execute();
    }

}
